package com.tucc.hello.leetcode.no1_400.no1_200.no199;

import com.tucc.hello.tree.Main;
import com.tucc.hello.tree.TreeNode;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode root = Main.makeTree(Arrays.asList(1, 2, 3, null, 5, null, 4, 8, null));
        List<Integer> result = solution.rightSideView(root);
        System.out.println(result);
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        result.add(root.val);
        rightSideView(result, root, 1);
        return result;
    }

    public void rightSideView(List<Integer> result, TreeNode root, int level) {
        if (root.right != null) {
            if (result.size() == level) {
                result.add(root.right.val);
            }
            rightSideView(result, root.right, level + 1);
        }
        if (root.left != null) {
            if (result.size() == level) {
                result.add(root.left.val);
            }
            rightSideView(result, root.left, level + 1);
        }
    }
}
